<?php


namespace App\Services;

use App\Models\SystemConfig;

class SystemConfigService
{
    /**
     * 获取设置信息
     * @return array
      [
          "website_name"    => "启程铁路档案管理",
          "website_short"   => "启程铁路",
          "website_keyword" => "这是网站关键字",
          "copyright"       => "这是版权说明",
          "copyright_url"   => "这是底部版权url",
      ]
     */
    public static function getInfo()
    {
        $list = SystemConfig::query()->select('key', 'value', 'describe')->get()->toArray();

        $configArr = [];
        foreach ($list as $val) {
            $configArr[$val['key']] = $val['value'];
        }

        return $configArr;
    }

    /**
     * 更新数据 只修改发生变化的配置
     */
    public function updateData($params)
    {
        foreach ($params as $key => $val) {
            $info = $systemConfig = SystemConfig::where('key', $key)->first();
            if ($info->value != $val) {
                $systemConfig->value = $val;
                $systemConfig->creator_id = user_id();
                $systemConfig->save();
            }
        }
        CommonService::addActionLog('修改系统配置信息');

        return true;
    }
}
